package com.example.phpclient.content;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.phpclient.DB.NotesDB;
import com.example.phpclient.Group;
import com.example.phpclient.R;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import android.util.Base64;

import org.json.JSONObject;

public class UploadPicDebug extends AppCompatActivity {

    private Button upload,takePic;
    private ImageView imgView;
    private File phoneFile;
    RequestQueue queue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
        setContentView(R.layout.activity_upload_pic_debug);
        queue = Volley.newRequestQueue(getApplicationContext());
        upload = (Button)findViewById(R.id.uploadPic);
        takePic = (Button)findViewById(R.id.takePic);
        imgView = (ImageView)findViewById(R.id.imgView);
        takePic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent iimg =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                phoneFile =new File(Environment.getExternalStorageDirectory().getAbsoluteFile()
                        +"/"+getTime()+".jpg");
                iimg.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(phoneFile));
                startActivityForResult(iimg,1);
            }
        });

        upload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = imageToBase64(phoneFile.getAbsolutePath());
                Log.d("base64",s);
                jsonRequest("img_upload",s);
            }
        });
    }
    private String getTime() {
        SimpleDateFormat format = new SimpleDateFormat("MMMM_dd_yyyy_h_mmaa");
        Date date = new Date();
        String str = format.format(date);
        return str;
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1)
        {//如果等于1代表传递的是一个图片
            Bitmap bitmap = BitmapFactory.decodeFile(phoneFile.getAbsolutePath());
            Log.d("base64",phoneFile.getAbsolutePath());
            imgView.setImageBitmap(bitmap);
        }
    }

    public static String imageToBase64(String path){
        if(TextUtils.isEmpty(path)){
            return null;
        }
        InputStream is = null;
        byte[] data = null;
        String result = null;
        try{
            is = new FileInputStream(path);
            //创建一个字符流大小的数组。
            data = new byte[is.available()];
            //写入数组
            is.read(data);
            //用默认的编码格式进行编码
            result = Base64.encodeToString(data,Base64.NO_WRAP);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(null !=is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return result;
    }

    public void jsonRequest(String aim,final String img){//通过组长编号查询教室
        String url ="http://192.168.16.76/imageBase64.php";

        HashMap<String, String> params = new HashMap<String ,String>();
        params.put("forwhat",aim);
        params.put("img",img);
        JSONObject jsonObject =new JSONObject(params);
        Log.d("base64",jsonObject.toString());
        JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        String s =jsonObject.toString();
                        Log.d("base64",s);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("base64","Error");
                    }

                });
        queue.add(jsonObjectRequest);
    }

}


